package ducna.android.aoeclip.screen.category;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import ducna.android.aoeclip.R;
import ducna.android.aoeclip.common.controller.BaseFragment;
import ducna.android.aoeclip.common.model.entity.Category;
import ducna.android.aoeclip.common.utils.Constant;
import ducna.android.aoeclip.common.utils.Logger;
import ducna.android.aoeclip.screen.category.adapter.CategoryListAdapter;
import ducna.android.aoeclip.screen.category.loader.DataCategoriesLoader;

/**
 * Created by DUCNA on 11/4/2014.
 */
public class CategoryFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<ArrayList<Category>>, Constant {
    public static final String TAG = CategoryFragment.class.getSimpleName();
    private View contentView;
    private ProgressDialog mProgressDialog;
    private RecyclerView mRecyclerViewCategory;
    private Bundle bundle = new Bundle();

    static ArrayList<Category> categoryArrayData = new ArrayList<Category>();
    // list category adapter
    private CategoryListAdapter mCategoryListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Logger.out(TAG, "onCreateView");
        contentView = inflater.inflate(R.layout.fragment_category, container, false);
//        findViewById(contentView);
        initData();
        return contentView;
    }

    public void findViewById(View contentView) {
        mRecyclerViewCategory = (RecyclerView) contentView.findViewById(R.id.recyclerViewListCategory);
        mRecyclerViewCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerViewCategory.setItemAnimator(new DefaultItemAnimator());

        // list category adapter
        mCategoryListAdapter = new CategoryListAdapter(categoryArrayData, R.layout.item_list_category, getActivity());
        mRecyclerViewCategory.setAdapter(mCategoryListAdapter);

    }

    public void initView(){
        bundle.putParcelableArrayList(ARGUMENT_CATEGORIES, categoryArrayData);
        Logger.out(TAG, "data category initView : " + categoryArrayData.size());
        this.setArguments(bundle);
    }

    public void initData() {
        initProgressDialog();
        if (categoryArrayData.size() == 0) {
            LoaderManager loaderManager = getActivity().getSupportLoaderManager();
            loaderManager.destroyLoader(LOADER_ID_CATEGORIES);
            loaderManager.initLoader(LOADER_ID_CATEGORIES, null, this);
        } else {
            findViewById(contentView);
        }
    }


    @Override
    public Loader<ArrayList<Category>> onCreateLoader(int i, Bundle bundle) {
        showProgressDialog();
        return new DataCategoriesLoader(getActivity());
    }

    @Override
    public void onLoadFinished(Loader<ArrayList<Category>> arrayListLoader, ArrayList<Category> categories) {
        hideProgressDialog();
        categoryArrayData.clear();
        categoryArrayData.addAll(categories);
        Logger.out(TAG, "categary size : " + categoryArrayData.size());
        mCategoryListAdapter.notifyDataSetChanged();
//        findViewById(contentView);
        initView();

    }

    @Override
    public void onLoaderReset(Loader<ArrayList<Category>> arrayListLoader) {

    }

    public abstract interface NavigationDrawerCallbacks{
        void onNavigationDrawerItemSelected(int position);
    }
}
